package com.java.admin.orders.controller;

import com.github.pagehelper.PageInfo;
import com.java.admin.makeanappointment.service.MakeanAppointmentService;
import com.java.admin.orders.service.OrdersService;
import com.java.database.entity.Makeanappointmentinfo;
import com.java.database.mapper.MakeanappointmentinfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private MakeanAppointmentService makeanAppointmentService;
    @Autowired
    private MakeanappointmentinfoMapper makeanappointmentinfoMapper;


    /**
     * 删除维修记录
     * @param orderid
     * @return
     */
    @RequestMapping("/removeOrders")
    public String removeOrders(String orderid){
        ordersService.deOrders(orderid);
        return "redirect:/admin/orders.html";
    }

    /**
     * 添加维修记录之前验证信息是否填写完整，选择维修员，在添加维修记录
     * @param makeanappointmentinfo
     * @param request
     * @return
     */
    @RequestMapping("/choiceemployee")
    public String choiceEmployee(Makeanappointmentinfo makeanappointmentinfo, HttpServletRequest request){
        String makeanappointmentid=makeanappointmentinfo.getMakeanappointmentid().toString();
        HttpSession session = request.getSession();
        Makeanappointmentinfo make = makeanAppointmentService.queryMakeanAppointmentById(makeanappointmentid);
        if(make.getMakeanappointmentcarno()==null||"".equals(make.getMakeanappointmentcarno())) {
            if(makeanappointmentinfo.getMakeanappointmentcarno()==null||"".equals(makeanappointmentinfo.getMakeanappointmentcarno())) {
                return "redirect:/admin/makeanappointmentinfo.html?makeanappointmentid="+makeanappointmentinfo.getMakeanappointmentid();
            }else{
                makeanAppointmentService.upAppointment(makeanappointmentinfo);
                session.setAttribute("make",makeanappointmentinfo);
                return "redirect:/admin/choiceemployee.html";
            }
        }
        session.setAttribute("make",make);
        return "redirect:/admin/choiceemployee.html";
    }

    /**
     * 添加维修记录
     * @param employeeid
     * @param request
     * @param makeanappointmentinfo
     * @return
     */
    @RequestMapping("/addOrders")
    public String addOrders(String employeeid, HttpServletRequest request,Makeanappointmentinfo makeanappointmentinfo){
        Makeanappointmentinfo make=null;
        HttpSession session = request.getSession();
        make = (Makeanappointmentinfo) session.getAttribute("make");
        session.removeAttribute("make");
        ordersService.addOrders(make,employeeid);
        return "redirect:/admin/orders.html";
    }
    /**
     * 查询所有维修记录
     * @param pageNum
     * @return
     */
    @RequestMapping("/queryAllOrders")
    @ResponseBody
    public PageInfo queryAll(String pageNum){
        PageInfo pageInfo = ordersService.queryAll(pageNum);
        return pageInfo;
    }
}
